<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog = true;isEdit = false"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120">
                <template v-slot="scope">
                  {{ scope.$index + 1 + (pageParams.page - 1) * pageParams.pagesize }}
                </template>
              </el-table-column>
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template v-slot="scope">
                  <el-button size="small" type="success" @click="doAssignPermission(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                background
                layout="sizes, prev, pager, next"
                :total="total"
                :page-size="pageParams.pagesize"
                :page-sizes="[2, 5, 10, 20]"
                :current-page="pageParams.page"
                @current-change="doPageChange"
                @size-change="doSizeChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 添加角色对话框 -->
    <el-dialog
      :title="(isEdit ? '编辑' : '新增') + '角色'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      @close="doClose"
    >
      <el-form ref="roleForm" :model="roleForm" :rules="roleFormRules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="addRole">确定</el-button>
          <el-button size="small" @click="doClose">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 分配权限对话框 -->
    <el-dialog title="分配权限" :visible.sync="showAssignDialog">
      <assignPermission :id="id" ref="assignPermissionRef" @assignPerSuccess="assignPerSuccess" />
    </el-dialog>
  </div>
</template>
<script>
import { getRoles, doDelRoles, doAddRoles, doEditRoles } from '@/api/settings'
import assignPermission from './assignPermission.vue'
export default {
  components: {
    assignPermission
  },
  data() {
    return {
      // 分页参数
      pageParams: {
        page: 1,
        pagesize: 10
      },
      // 表格数据
      list: [],
      // 总条数
      total: 1,
      // 对话框布尔值
      showDialog: false,
      // 对话框表单
      roleForm: {
        name: '',
        description: ''
      },
      // 对话框表单校验
      roleFormRules: {
        name: [
          { required: true, message: '角色名称不可为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不可为空', trigger: 'blur' }
        ]
      },
      // 是否是编辑
      isEdit: false,
      // 分配权限布尔值
      showAssignDialog: false,
      // 被点击的角色的id
      id: ''
    }
  },
  created() {
    this.loadRoles()
  },
  // methods:事件处理函数
  methods: {
    // 加载角色列表
    async loadRoles() {
      const res = await getRoles(this.pageParams)
      // 将返回的数据绑定到data中
      this.list = res.data.data.rows
      this.total = res.data.data.total
    },
    // 页码变化
    doPageChange(page) {
      this.pageParams.page = page
      this.loadRoles()
    },
    // 每页页数变化
    doSizeChange(pagesize) {
      this.pageParams.pagesize = pagesize
      this.pageParams.page = 1
      this.loadRoles()
    },
    // 删除角色
    delRole(id) {
      this.$confirm('你确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await doDelRoles(id)
        // 如果是当前页面最后一条数据删除后就页码减1且如果是第1页就不用减
        if (this.total % this.pageParams.pagesize === 1 && this.pageParams.page !== 1) {
          this.pageParams.page--
        }
        this.loadRoles()
      }).catch(() => {})
    },
    // 新增/编辑角色
    async addRole() {
      // 编辑角色
      if (this.isEdit) {
        await doEditRoles(this.roleForm)
        this.showDialog = false
        this.loadRoles()
      } else { // 新增角色
        await doAddRoles(this.roleForm)
        this.showDialog = false
        // 调完接口total要加1
        this.total++
        // 如果当前页面刚好装满新增的角色要新起一页 页码直接跳到最后1页
        this.pageParams.page = Math.ceil(this.total / this.pageParams.pagesize)
        this.loadRoles()
        this.roleForm = {
          name: '',
          description: ''
        }
      }
    },
    // 关闭对话框
    doClose() {
      this.showDialog = false
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 编辑角色
    editRole(row) {
      this.isEdit = true
      this.showDialog = true
      this.roleForm = { ...row }
    },
    // 分配权限
    doAssignPermission(id) {
      this.showAssignDialog = true
      this.id = id
      this.$nextTick(() => {
        this.$refs.assignPermissionRef.loadRolePermission()
      })
    },
    // 分配权限完成
    assignPerSuccess() {
      this.showAssignDialog = false
      this.loadRoles()
    }
  }
}
</script>
